const db = require('../../db.config')
const Sequelize = db.sequelize
const Op = Sequelize.Op

const focusDetail = Sequelize.import('../../schema/recommend/focusDetail')

focusDetail.sync({force: false})

class FocusDetailModule {
  /**
   * 添加关注
   */
  static async setFocus(data) {
    focusDetail.create({
      detailId: data.detailId,
      userId: data.userId,
      createAt: new Date
    })
  }

  /**
   * 取消关注
   */
  static async unFocus(where) {
    focusDetail.destroy({
      where
    })
  }

  /**
   * 查询关注
   */
  static async getFocus(where, order) {
    let sql = {}
    if (!order) {
      sql = {
        where,
        raw: true
      }
    } else {
      sql = {
        where,
        raw: true,
        order: [
          [order, 'DESC']
        ]
      }
    }
    return await focusDetail.findAll(sql)
  }
}

module.exports = FocusDetailModule
